[assembly: WebActivator.PostApplicationStartMethod(typeof(Tracker.WCF.App_Start.SimpleInjectorInitializer), "Initialize")]

namespace Tracker.WCF.App_Start
{
    using System.Reflection;

    using SimpleInjector;
    using SimpleInjector.Extensions;
    using SimpleInjector.Integration.Wcf;
    using Tracker.Data.DataContext;
    using Tracker.Data.UnitOfWork;
    using Tracker.Data.Repository;
    using Tracker.Services.Abstract;
    using Tracker.Services.Implementation;

    public static class SimpleInjectorInitializer
    {
        /// <summary>Initialize the container and register it for the WCF ServiceHostFactory.</summary>
        public static void Initialize()
        {
            // Did you know the container can diagnose your configuration? Go to: http://bit.ly/YE8OJj.
            var container = new Container();

            container.EnablePerWcfOperationLifestyle();
            
            InitializeContainer(container);

            container.RegisterWcfServices(Assembly.GetExecutingAssembly());

            container.Verify();
            
            SimpleInjectorServiceHostFactory.SetContainer(container);

            // TODO: Add the following attribute to all .svc files:
            // Factory="SimpleInjector.Integration.Wcf.SimpleInjectorServiceHostFactory, SimpleInjector.Integration.Wcf"
        }

        private static void InitializeContainer(Container container)
        {
            // Data Layer Bindings

            container.Register<IDataContext, DataContext>(Lifestyle.Singleton);
            container.Register<IUnitOfWork, UnitOfWork>();
            container.RegisterOpenGeneric(typeof(IRepository<>), typeof(Repository<>));

            // Service LAyer Bindings

            container.Register<IPositionService, PositionService>();
            container.Register<IUserService, UserService>();
        }
    }
}